package com.app.twth.ui.lpnphoto;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityLpnPhotoBinding;
import com.app.twth.databinding.DialogLpnphotoContainerDetailBinding;
import com.app.twth.ui.lpnphoto.lpn.LPNPhotoLPNActivity;
import com.app.twth.ui.tally.Tally;
import com.app.twth.utils.DividerItemDecoration;
import com.app.twth.utils.MyDialogUtil;

import java.util.ArrayList;
import java.util.List;

public class LPNPhotoActivity extends BaseActivity<ActivityLpnPhotoBinding, LPNPhotoViewModel> {
    private String TAG = getClass().getSimpleName();

    private int type;
    private Tally tally;
    private long timeMillis;
    private LPNPhotoAdapter adapter;
    private List<Tally> tallyList = new ArrayList<>();


    @Override
    protected void initListener() {
        viewModel.getPutawayContainer();
        viewModel.putawayContainerLiveData.observe(this, data -> {
            if (type == 0) {
                tallyList.clear();
                tallyList.addAll(data);
                adapter.setData(data, type);
            }
        });
        viewModel.tallyDetailsLiveData.observe(this, data -> {
            showTallyDetailDialog(data);
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    @Override
    protected String setTitle() {
        return "LPNPhoto(Container)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext("");

        binding.tvCode.requestFocus();

        adapter = new LPNPhotoAdapter(this, tallyList, position -> {
            if (tallyList.size() <= position) {
                return;
            }
            tally = tallyList.get(position);
            Bundle bundle = new Bundle();
            bundle.putSerializable("Tally", tally);
            AppRouter.showFragmentActivity(mContext, LPNPhotoLPNActivity.class, bundle);
        }, position -> {
            tally = tallyList.get(position);
            viewModel.getPutawayDetail(tally.getOrderId());
        });
        binding.recyclerview.setHasFixedSize(true);
        binding.recyclerview.setItemAnimator(new DefaultItemAnimator());

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerview.setLayoutManager(layoutManager1);
        //添加装饰类
        binding.recyclerview.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
        binding.recyclerview.setAdapter(adapter);

        binding.tvCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
//                setSearchData();
            }
        });
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_reset:
                binding.tvCode.setText("");
                break;
        }
    }


    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
    }

    private AlertDialog dialogTallyDetail;

    private void showTallyDetailDialog(List<LPNPhotoDetails> data) {
        dialogTallyDetail = MyDialogUtil.getDialog(mContext, initTallyDetailDialogView(data), Gravity.CENTER);
        dialogTallyDetail.setCanceledOnTouchOutside(true);
        dialogTallyDetail.show();
    }

    LPNPhotoDetailAdapter tallyDetailAdapter;

    private View initTallyDetailDialogView(List<LPNPhotoDetails> data) {
        DialogLpnphotoContainerDetailBinding dialogBinding = DialogLpnphotoContainerDetailBinding.inflate(getLayoutInflater());
        dialogBinding.tvTitle.setText("Tally (" + tally.getContainerNo() + ")");
        tallyDetailAdapter = new LPNPhotoDetailAdapter(this, data, position -> {
        }
        );

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        dialogBinding.recyclerviewDetail.setLayoutManager(layoutManager1);
        dialogBinding.recyclerviewDetail.setAdapter(tallyDetailAdapter);

        dialogBinding.tvCancel.setOnClickListener(v -> {
            if (dialogTallyDetail != null) {
                dialogTallyDetail.dismiss();
            }
        });
        return dialogBinding.getRoot();
    }
}
